package com.shanghaionstar.home.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.PublicService;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.mag.publicService.ReqBannerEntity;
import com.onstar.cn.mag.publicService.response.ResponBannerEntity;
import com.onstar.cn.smart.entity.ServiceItem;
import com.shanghaionstar.enumtype.OSTypeEnum;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/6/29.
 */
public class HomeService extends BaseService<List<ResponBannerEntity>> {
    private Context context;

    public HomeService(Context context) {
        this.context = context;
    }


    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public List<ResponBannerEntity> jsonToBean(String json) {
        return new Gson().fromJson(json,new TypeToken<ArrayList<ResponBannerEntity>>(){}.getType());
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public String commandDataJson() throws OnStarException{
        PublicService publicService = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        ReqBannerEntity entity = new ReqBannerEntity();
        if (!CommonUtil.isNull(ApplicationUtil.getInstance().getCurrentVehicle())){
            entity.setVin(ApplicationUtil.getInstance().getCurrentVehicle().getVin());
            entity.setMake(ApplicationUtil.getInstance().getCurrentVehicle().getMake());
            entity.setModel(ApplicationUtil.getInstance().getCurrentVehicle().getModel());
            entity.setYear(ApplicationUtil.getInstance().getCurrentVehicle().getYear());
        }
        entity.setDeviceType("10");
        entity.setDeviceOS(OSTypeEnum.ANDROID.value());
        entity.setLanguagePreference(DeviceUtil.getLanguage().equalsIgnoreCase("en") ? Language.EN.value() : Language.ZH.value());
        entity.setUserID(ApplicationUtil.getInstance().getIdpID());
        entity.setImgType(OSTypeEnum.ANDROID.value());
        int versionCode = new DeviceUtil(context).getAppVersionCode();
        entity.setVersionCode(versionCode + "");
        requestEntity = publicService.getBanner(requestEntity, entity);
        requestEntity.setUrl(requestEntity.getUrl());
        requestEntity = HttpInjectUtils.injectHttp(HomeService.this.getClass(), "commandDataJson", requestEntity);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    //    JsonParseUtil.JsonToBean(ResponBannerEntity.class, json)
    public Observable<List<ResponBannerEntity>> commandData() {
        return Observable.defer(new Func0<Observable<List<ResponBannerEntity>>>() {
            @Override
            public Observable<List<ResponBannerEntity>> call() {
                try {
                    List<ResponBannerEntity> result = new Gson().fromJson(commandDataJson(),new TypeToken<List<ResponBannerEntity>>(){}.getType());
                    return Observable.just(result);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getServiceStatusJson(RequestEntity entity, String smartDriver) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.getServiceStatus(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), smartDriver);
        addAuthorization(context, requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(HomeService.this.getClass(), "getServiceStatusJson", requestEntity, RequestEntity.class, String.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    private void addAuthorization(Context context, RequestEntity requestEntity) {
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
    }

    //     List<ServiceItem> items = new Gson().fromJson(json, new TypeToken<ArrayList<ServiceItem>>() {
//}.getType());
    public Observable<List<ServiceItem>> getServiceStatus(final RequestEntity entity, final String smartHome) {
        return Observable.defer(new Func0<Observable<List<ServiceItem>>>() {
            @Override
            public Observable<List<ServiceItem>> call() {
                try {
                    List<ServiceItem> items = new Gson().fromJson(getServiceStatusJson(entity, smartHome), new TypeToken<ArrayList<ServiceItem>>() {
                    }.getType());
                    return Observable.just(items);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }



}
